package cn.wolfcode.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wolfcode.crm.domain.ProductStock;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductStockService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PageUtils;


@Service
public class ProductStockServiceImpl implements IProductStockService{
	@Autowired
	private ProductStockMapper productstockMapper;
	
	public void save(ProductStock productstock) {
		productstockMapper.insert(productstock);
	}

	public void update(ProductStock productstock) {
		productstockMapper.updateByPrimaryKey(productstock);
	}

	public void delete(Long id) {
		productstockMapper.deleteByPrimaryKey(id);
	}

	public PageResult query(QueryObject qo) {
		PageResult result = PageUtils.query(qo,productstockMapper);
		@SuppressWarnings("unchecked")
		List<ProductStock> rows = (List<ProductStock>) result.getRows();
		for (ProductStock ps : rows) {
			String unit = ps.getProduct().getUnit();
			ps.getProduct().setUnit(unit +"*" + ps.getStoreNumber());
		}
		return result;
	}

	public ProductStock selectByDepotIdAndProductId(Long dId, Long pId) {
		return productstockMapper.selectByDepotIdAndProductId(dId, pId);
	}
}
